<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Trade;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use DB;

class CustomerController extends AdminController
{
    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 我的客户
     */
    public function index(Request $request){
        $info= $request->user();
        $where=array();
        if(!empty($request->name)){
            $where[]=array(DB::raw("concat(firstname,lastname)"),'like',"%".$request->name."%");
        }
        if(!empty($request->email)){
            $where[]=array("users.email",'like',"%".$request->email."%");
        }
        if(!empty($request->phone)){
            $where[]=array("users.phone",'like',"%".$request->phone."%");
        }
        $list = User::where("sorts",'like','-'.$info->id."%")->where($where)->orderby("id","desc")->paginate(20);
        $append = array();
        $re = $request->toArray();
        foreach ($re as $key => $value) {
            $append[$key] = $value;
        }
        $list->appends($append);
        return view("customer.list",['list'=>$list]);

    }

    /**
     * 客户升级为代理
     */

    public function upgrade(Request $request,$id){

        if($request->isMethod("post")){
            $email=$request->email;
            $info = $request->user();
            $ecount = Admin::where("email",$email)->count();
            $ucount = User::where("email",$email)->where("act",'1')->count();
            if($ecount>=1){
                return back()
                    ->withErrors(array("邮箱已经被注册"))
                    ->withInput();
            }elseif($info->percent<$request->percent) {
                return back()
                    ->withErrors(array("提成不能超过上级代理"))
                    ->withInput();
            }elseif($ucount>=1) {
                return back()
                    ->withErrors(array("不需要升级为代理"))
                    ->withInput();
            }else{
                $admin = new Admin();
                $admin->name = $request->name;
                $admin->email = $email;
                $admin->phone = $request->phone;
                $admin->card = $request->card;
                $admin->code = $this->getcode();
                $admin->password = bcrypt($email);
                $admin->pid = $info->id;
                $admin->percent = $request->percent;
                $email = $request->email;
                if ($admin->save()) {
                    $admin->sorts = $info->sorts . '-' . $admin->id;
                    $admin->save();
                    $user=User::where("email",$email)->first();
                    $user->act='1';
                    $user->save();
                    /**
                     * 邮件通知 登录地址和用户名密码
                     */
                    $url=$_SERVER["HTTP_HOST"].'/login';
                    $content = "恭喜你成为金融代理系统的代理，登录地址：".$url."，用户名:" . $email . "，密码:" . $email;
                    Mail::raw($content, function ($message) use ($email) {
                        $message->subject(config("app.host"));
                        $message->to($email);
                    });

                    $this->sendsms($request->phone,'SMS_62895109');

                    $this->setlog(json_encode($admin),'代理将客户" '.$request->name.'" 升级为下级代理');

                    return redirect("/agent");
                } else {
                    return back()->withErrors()->withInput();;
                }
            }

        }else{
            $user = User::find($id);
            return view("customer.add",["user"=>$user]);
        }


    }

    public function getcode(){
        $pattern='1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ';
        $key='';
        for($i=0;$i<10;$i++)
        {
            $key .= $pattern{mt_rand(0,35)};    //生成php随机数
        }
        $count= Admin::where("code",$key)->count();
        if($count){
            $key=$this->getcode();
        }
        return $key;
    }

    /**
     * 查看客户交易记录
     */

    public function lists($id){

        $list = Trade::where("tranid",$id)->orderby("id","desc")->paginate(20);

        return view("customer.tran_list",['list'=>$list]);




    }


}
